/**
 * Copyright (c) 2020-2024, huli.com
 * All rights reserved.
 */
package com.xiaohujr.xuexue.leetcode.interview.linklist;

import com.xiaohujr.xuexue.leetcode.tree.ListNode;

/**
 * https://leetcode-cn.com/problems/linked-list-cycle/
 *
 * @author Xue Xue (xuexue1@huli.com)
 * @version 1.0
 * @since 2021/1/14
 */
public class HasCycle {

    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (slow != fast) {
            if (fast == null || fast.next == null) {
                return false;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return true;
    }

}
